<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>unused</title>
    <style>
      html, body, iframe {
        height: 100%;
      }
      body {
        margin: 0;
      }
      iframe {
        width: 100%;
        border: 0;
        display: block;
      }
    </style>
  </head>
  <body>
    <iframe id="OLM" title="OpenLayers Map"></iframe>
    <script>
      /**
       * Plugin embedding the littlenavmap-openlayers implementation
       * 
       * See: 
       * - https://github.com/KOKAProduktion/littlenavmap-openlayers
       */

      onmessage = event => {
        let message = event.data.pluginParent;
        if(message) {
          switch(message.cause) {
            case "callback":
              return handleEvents(message.cargo);
            case "cleanup":
              return stop();
            case "commence":
              var OLM = document.getElementById("OLM");
              OLM.onload = lnm;
              return OLM.src = "ol/index.html";
            default:
          }
        }
      };

      function stop() {}
      
      function nop() {}
      
      var doICAOSearch = nop;
      
      var latestInput = "";
      
      function handleEvents(cargo) {
        switch (cargo.id) {
          case "icaot":
            switch (cargo.event) {
              case "input":
                latestInput = cargo.value;
                break;
              case "keydown":
                evalICAOSearch (cargo.data[0]);
                break;
              default:
            }
            break;
          case "icaob":
            switch (cargo.event) {
              case "click":
                doICAOSearch ();
                break;
            }
          default:
        }
      }
      
      function evalICAOSearch(key) {
        key === "Enter" ? doICAOSearch () : !1;
      }
        
      var littlenavmap;
      
      function reallyDoICAOSearch () {
        fetch("/api/airport/info?ident=" + latestInput).then(response => response.json(), error => console?.log(error)).then(json => {
          // turn off aircraft follow
          if (littlenavmap.following) {
            littlenavmap.map.getControls().getArray()[0].handleFollow();
          }
          // move to airport position
          littlenavmap.moveTo([json.position.lon, json.position.lat]);
        }, error => console?.log(error)).catch(error => console?.log(error));
      }

      function lnm() {
        littlenavmap = this.contentWindow.littlenavmap;
        doICAOSearch = reallyDoICAOSearch;
      }
    </script>
  </body>
</html>